package ch.unisi.inf.pfii.teamred.pacman.model;

import java.util.ArrayList;

/**
 * @author luca.vignola@lu.unisi.ch
 * 
 */
public abstract class TraversableBlock implements Block {

	private final ArrayList<Item> items;
	private final ArrayList<Creature> creatures;

	public TraversableBlock() {
		items = new ArrayList<Item>();
		creatures = new ArrayList<Creature>();
	}

	public TraversableBlock(final Item item) {
		items = new ArrayList<Item>();
		creatures = new ArrayList<Creature>();
		items.add(item);
	}

	public final ArrayList<Item> getItems() {
		return items;
	}

	public final ArrayList<Creature> getCreatures() {
		return creatures;
	}

	public final void addItem(final Item item) {
		items.add(item);
	}

	public final void removeItem(final Item item) {
		items.remove(item);
	}

	public final void addCreature(final Creature creature) {
		creatures.add(creature);
	}

	public final void removeCreature(final Creature creature) {
		creatures.remove(creature);
	}

	public final boolean isTraversable() {
		return true;
	}

	public final boolean containsItem() {
		return items.size() != 0;
	}

	public final boolean containsPacman() {
		for (final Creature creature : creatures) {
			if (creature.isControlledByPlayer()) {
				return true;
			}
		}

		return false;
	}

	public final Pacman getPacman() {
		for (final Creature creature : creatures) {
			if (creature.isControlledByPlayer()) {
				return (Pacman) creature;
			}
		}

		return null;
	}

	public final boolean containsAiControlledCreature() {
		for (final Creature creature : creatures) {
			if (!creature.isControlledByPlayer()) {
				return true;
			}
		}

		return false;
	}

	public final boolean collisionHappened() {
		return containsAiControlledCreature() && containsPacman();
	}

	public final String toString() {
		if (creatures.size() != 0) {
			return creatures.get(0).toString();
		} else if (items.size() != 0) {
			return items.get(0).toString();
		} else {
			return " ";
		}
	}
}
